package com.qining618.common.utils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.Data;
import org.apache.commons.collections4.MapUtils;

import java.util.Map;

/**
 * 查询参数
 *
 * @author iqiao
 * @date 2018/5/3.
 */
@Data
public class Query<T> {

    private IPage<T> page;
    private QueryWrapper<T> queryWrapper;

    public Query(Map<String, Object> params) {
        int pageNo = MapUtils.getIntValue(params, "pageNo", 1);
        int pageSize = MapUtils.getIntValue(params, "pageSize", 10);
        this.page = new Page<>(pageNo, pageSize);
        params.remove("pageNo");
        params.remove("pageSize");

        queryWrapper = new QueryWrapper<>();
        for (Map.Entry<String, Object> entry : params.entrySet()) {
            if (entry.getValue() == null) {
                continue;
            }
            Object value = entry.getValue();
            if (entry.getValue() instanceof String) {
                if (StringUtils.isNotBlank(entry.getValue().toString())) {
                    value = StringUtils.trim(entry.getValue().toString());
                } else {
                    continue;
                }
            }
            String[] fields = entry.getKey().split("_");
            if (fields.length < 2) {
                queryWrapper.eq(StringUtils.humpToLine(fields[0]), value);
            } else {
                String condition = fields[0];
                String field = StringUtils.humpToLine(fields[1]);

                switch (condition) {
                    case "like":
                        queryWrapper.like(field, value);
                        break;
                    case "likeLeft":
                        queryWrapper.likeLeft(field, value);
                        break;
                    case "likeRight":
                        queryWrapper.likeRight(field, value);
                        break;
                    case "gt":
                        queryWrapper.gt(field, value);
                        break;
                    case "ge":
                    case "gte":
                        queryWrapper.ge(field, value);
                        break;
                    case "lt":
                        queryWrapper.lt(field, value);
                        break;
                    case "le":
                    case "lte":
                        queryWrapper.le(field, value);
                        break;
                    case "ne":
                    case "neq":
                        queryWrapper.ne(field, value);
                        break;
                    case "eq":
                    default:
                        queryWrapper.eq(field, value);
                        break;
                }
            }
        }
    }

    public static <T> Page<T> of(Map<String, Object> params) {
        int pageNo = MapUtils.getIntValue(params, "pageNo", 1);
        int pageSize = MapUtils.getIntValue(params, "pageSize", 10);
        params.remove("pageNo");
        params.remove("pageSize");
        return new Page<T>(pageNo, pageSize);
    }

}
